Public Class UCOutPieces
  Public pieces_out(11) As Panel
  Public pieces_num(11) As Label

  Enum PieceType
    Blue
    Red
  End Enum
  Const SpaceX As Integer = 10
  Const SpaceY As Integer = 30
  Dim mPieceType As PieceType
  Property Type() As PieceType
    Get
      Return mPieceType
    End Get
    Set(ByVal value As PieceType)
      mPieceType = value
      LoadOutPieces()
    End Set
  End Property
  Private Sub LoadOutPieces()
    ' define pieces out of board
    pieces_out(0) = piece_out
    pieces_out(0).Size = PieceSize
    pieces_num(0) = piece_num
    Dim fImageRes As String
    Me.SuspendLayout()
    For i As Integer = 0 To 11
      If i > 0 Then
        Debug.Assert(pieces_out(i) Is Nothing, "Piece already created")
        pieces_out(i) = New Panel
      End If
      pieces_out(i).Name = Type & "_out"
      pieces_out(i).Tag = i
      pieces_out(i).Size = PieceSize
      fImageRes = pionia(i).fname & IIf(Type = PieceType.Blue, "_1", "_2") '.gif
      pieces_out(i).BorderStyle = piece_out.BorderStyle
      pieces_out(i).BackgroundImage = My.Resources.ResourceManager.GetObject(fImageRes)  'System.Drawing.Image.FromFile(fblue)
      pieces_out(i).BackgroundImageLayout = ImageLayout.Stretch
      pieces_out(i).Visible = True
      AddHandler pieces_out(i).Click, AddressOf out_click
      ToolTip1.SetToolTip(pieces_out(i), pionia(i).desc)
      ' show remaining pieces
      If i > 0 Then pieces_num(i) = New Label
      pieces_num(i).Name = Type & "_num"
      pieces_num(i).Size = piece_num.Size
      pieces_num(i).Font = piece_num.Font
      pieces_num(i).ForeColor = IIf(Type = PieceType.Blue, Color.Blue, Color.Red)
      pieces_num(i).Text = CStr(typepieces(i))
      pieces_num(i).Visible = True : pieces_num(i).Enabled = True
      If i > 0 Then
        GroupBox1.Controls.Add(pieces_out(i))
        GroupBox1.Controls.Add(pieces_num(i))
      End If
      RestoreOutPiecePosition(i)
    Next
    Me.GroupBox1.Text = Type.ToString()
    Me.GroupBox1.ForeColor = IIf(Type = PieceType.Blue, Color.Blue, Color.Red)
    Me.Size = New Size(PosX(4), PosY(3))
    Me.ResumeLayout()
  End Sub
  Private Function PosX(ByVal j As Integer) As Integer
    Return SpaceX + j * (SpaceX + PieceSize.Width)
  End Function
  Private Function PosY(ByVal i As Integer) As Integer
    Return SpaceY + i * (SpaceY + PieceSize.Height)
  End Function
  Sub RestoreOutPiecePosition(ByRef index As Integer)
    Dim outpiece As Panel = pieces_out(index)
    Dim outlabel As Label = pieces_num(index)
    '   outpiece.Left = CInt(Mid(outpiece.Tag, 1, 4))
    '  outpiece.Top = CInt(Mid(outpiece.Tag, 6, 4))
    Dim newX, newY As Integer
    newX = PosX(index Mod 4) : newY = PosY(Math.Floor(index / 4))
    outpiece.Location = New Point(newX, newY)
    outlabel.Location = New Point(newX + PieceSize.Width \ 2 - 3, _
     newY + PieceSize.Height + 2)
    outpiece.ForeColor = IIf(Type = PieceType.Blue, Color.Blue, Color.Red)
    outpiece.Visible = True
  End Sub
  Sub ZeroPiecesLabels()
    Dim i As Integer
    For i = 0 To 11
      pieces_out(i).Text = "0"
    Next i
  End Sub
  Sub RemoveOutPieceLabels()
    Dim i As Integer
    For i = 0 To 11
      pieces_out(i).Visible = False
    Next i
  End Sub
  'Private Sub redout_MouseDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.MouseEventArgs)
  '  Dim Button As Short = eventArgs.Button \ &H100000
  '  Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys \ &H10000
  '  Dim Index As Short = Array.IndexOf(redout, eventSender)
  '  Dim i As Integer
  '  If ArrangeMode = 1 And GameMode = enumGameMode.ARRANGEMENT And CurrentPlayer = 2 Then
  '    For i = 0 To 11
  '      mFormPiecesOut.panRed.pieces_num(i).ForeColor = Color.Red
  '    Next i
  '    mFormPiecesOut.panRed.pieces_num(Index).ForeColor = Color.FromArgb(255, 0, 255, 0)
  '    CurrentType = Index
  '  End If
  'End Sub
  Private Sub out_click(ByVal Sender As System.Object, ByVal eventArgs As System.EventArgs)  'System.Windows.Forms.MouseEventArgs)
    ' Dim Button As Short = eventArgs.Button \ &H100000
    ' Dim Shift As Short = System.Windows.Forms.Control.ModifierKeys 
    Dim Index As Short = CType(Sender, Panel).Tag ' Array.IndexOf(pieces_out, Sender)
    If Index < 0 Then Exit Sub
    Dim i As Integer
    If ArrangeMode = 1 AndAlso GameMode = enumGameMode.ARRANGEMENT Then
      'AndAlso CurrentPlayer = 1
      ' Active kind of piece shown in green, all others in blue
      For i = 0 To 11
        pieces_num(i).ForeColor = IIf(Type = PieceType.Blue, Color.Blue, Color.Red)
      Next i
      pieces_num(Index).ForeColor = Color.Green
      CurrentType = Index
    End If
  End Sub
  Sub StoreOutPiecesPositions()
    Dim i As Integer
    For i = 0 To 11
      pieces_out(i).Tag = MakeNumNLen(pieces_out(i).Left, 4) & "," & MakeNumNLen(pieces_out(i).Top, 4)
    Next i
  End Sub
  Sub RemoveOutPieces()
    Dim i As Integer
    For i = 0 To 11
      pieces_out(i).Visible = False
    Next i
  End Sub
  ' put out pieces back to their original positions
  Sub RestoreOutPiecePositions()
    Dim i As Integer
    For i = 0 To 11
      Call RestoreOutPiecePosition(i)
    Next i
  End Sub

  Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

  End Sub

  Private Sub UCOutPieces_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'LoadOutPieces()
    'StoreOutPiecesPositions()
  End Sub
End Class
